Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lint/noEmptyBlockStatements): add rule #521

Merged
merged 4 commits into from
Oct 16, 2023

Conversation

andrewangelle
Copy link
Contributor

@andrewangelle andrewangelle commented Oct 13, 2023

Summary

Implements no empty and no empty static block under 1 new rule for noEmptyBlockStatements

Addresses issue: closes #39

Test Plan

Added a variety of snapshot tests included in the PR

@github-actions github-actions bot added A-Project Area: project A-Linter Area: linter A-Website Area: website L-JavaScript Language: JavaScript and super languages A-Diagnostic Area: diagnostocis labels Oct 13, 2023
Copy link
Member

@Conaclos Conaclos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a first good shot! I left some comments. :)

Comment on lines 23 to 59
/// ```js,expect_diagnostic
/// function foo () {}
///
/// const foo = () => {}
///
/// function fooWithNestedEmptyBlock() {
/// let a = 1;
/// function shouldFail(){}
/// return a
/// }
///
/// const fooWithNestedEmptyBlock = () => {
/// let a = 1;
/// const shouldFail = () => {}
/// return a
/// }
/// let someVar;
/// if (someVar) {
/// }
///
/// while (someVar) {
/// }
///
/// switch(someVar) {
/// }
/// try {
/// doSomething();
/// } catch(ex) {
///
/// } finally {
///
/// }
///
// class Foo {
// static {}
// }
/// ```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every example that emits a diagnostic should be in its own code snippet.
By the way, we don't need to be exhaustive. You could choose a few examples that demonstrates the scope of the rule.

///
/// ## Valid
///
/// ```js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here: we should keep only a few examples.

Comment on lines 145 to 149
if is_empty && !has_comments {
return Some(text_range);
}

None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if is_empty && !has_comments {
return Some(text_range);
}
None
(is_empty && !has_comments).then_some(())


impl Rule for NoEmptyBlockStatements {
type Query = Ast<Query>;
type State = TextRange;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we use the range of the queried node, I think we can use () as state and directly call query.range() in diagnostics (see proposed changes).

Suggested change
type State = TextRange;
type State = ();

Some(
RuleDiagnostic::new(
rule_category!(),
state,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

query.range() internally calls query.syntax().text_trimmed_range().

Suggested change
state,
query.range(),

rule_category!(),
state,
markup! {
"No empty blocks allowed."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"No empty blocks allowed."
"Unexpected empty block."

},
)
.note(markup! {
"Empty static blocks and block statements, while not technically errors, usually occur due to refactoring that wasn’t completed. They can cause confusion when reading code."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We recently updated the analyzer's contributing guide to help to write a diagnostic. We should suggest a way to fix the issue. Also, I propose some simplification:

Suggested change
"Empty static blocks and block statements, while not technically errors, usually occur due to refactoring that wasn’t completed. They can cause confusion when reading code."
"Empty blocks are usually the result of an incomplete refactoring. Remove the empty block or add a comment inside it if it is intentional."

static {}
}

for(let i; i>0; i++){}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last thing before merging: do you think it makes sense adding an exception for all loop statements?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you think of a concrete and/or common example where a developer wants to execute a loop without doing anything in the iterations?

My initial thought is that would be pretty uncommon, and they could just inline suppress the rule or comment in the block if they really wanted it there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just my two cents. I'm not opposed to adding an exception if you think it's appropriate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's ship the rule as is. We see later if users complain about the default behavior.

@Conaclos Conaclos merged commit 7b6520b into biomejs:main Oct 16, 2023
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Diagnostic Area: diagnostocis A-Linter Area: linter A-Project Area: project A-Website Area: website L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

📎 Implement lint/noEmptyBlockStatements - eslint/no-empty eslint/no-empty-static-block
2 participants